/*
 * PsMediaReader.h
 *
 *  Created on: 2018年9月1日
 *      Author: zhengboyuan
 */

#ifndef PSMEDIAREADER_H_
#define PSMEDIAREADER_H_

#include "MediaReader.h"
#include "MStorageType.h"
#include <vector>
#include "PsParser.h"
#include "PsFileStream.h"


namespace av
{


class PsMediaReader: public MediaReader
{
public:
	PsMediaReader();
	virtual ~PsMediaReader();

	PsMediaReader(MRecord* records, int size);

	void setRecords(MRecord* records, int size);

	virtual int open(const std::string& url, const std::string& params);

	virtual void close();

	virtual bool isOpen();

	virtual bool getFormat(MediaFormat& fmt);

	virtual int getDuration();

	virtual int play();
	virtual int pause();
	virtual void stop();

	virtual int getState();

	virtual bool seekable();

	virtual int seek(int64_t offset);

	virtual int64_t getTime();

	virtual int setScale(float scale);

	virtual float getScale();

	virtual int read(AVPacket& pkt);

	virtual void interrupt();

	virtual bool isLive();

protected:
	typedef std::vector< MRecord >	MRecordArray;


protected:
	FILE* openNextFile();

	bool getMediaFormat(FILE* file, MediaFormat& fmt);

	void closeFile();

	int countDuration(const MRecordArray& records, size_t offset);

	MRecord getCurrentRecord();

	const char* getCurrentFilePath();

	int findRecord(int64_t offset, int64_t& subPffset);

	bool seekRecord(int index, int64_t offset);

	bool seekRecord(const MRecord& record, FILE* file, int64_t offset);

	FILE* openNextFile(int& fromIndex);

	void clearCache();

	bool readMediaPacket(PsFileStream& psFileStream, PsPacket& packet);
	bool findBackPacket(PsFileStream& psFileStream, int64_t fileSize, PsPacket& packet);

protected:
	MRecordArray	m_records;
	int		m_index;

	av::PsParser	m_psParser;

	FILE*	m_file;

	av::MediaFormat	m_format;
	int64_t	m_pts;

	int64_t	m_startPts;

};


} /* namespace av */

#endif /* PSMEDIAREADER_H_ */
